import boxCollider from "./boxCollider";
import Common from "./Common";

export default class gameUI extends Laya.Script {
    public qiusImage = [
        "Image/绿球 拷贝 2.png",
        "Image/橙色球.png",
        "Image/篮球 拷贝.png",
        "Image/褐色球 拷贝.png",
        "Image/黄球.png"
    ]
    public clawOpen = [
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01XuMzHQ1EUdYH8jXn3_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i1/2185320355/O1CN01bBVNmt1EUdYKWiOuQ_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01TW3eFC1EUdY8vzVID_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i1/2185320355/O1CN01mqIglL1EUdYGM80Vv_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01N3glfb1EUdYJSgCXg_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01ZDyEBU1EUdYEoKDXS_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01r6sbYX1EUdYEoLYg5_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i1/2185320355/O1CN01qORagc1EUdYI3INXp_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01gbM2yk1EUdYLBQSPL_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i2/2185320355/O1CN01ApsEiM1EUdYJlumUq_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i2/2185320355/O1CN01v9b3jh1EUdYJSenEk_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i2/2185320355/O1CN01cH48BK1EUdYGSDAJj_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01ue6Y591EUdYGM7bZe_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN016Jh1eN1EUdYGd3e1x_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01Kc1oZU1EUdYLBROdC_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i2/2185320355/O1CN01gmduHH1EUdYH8j4i2_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i1/2185320355/O1CN018hg68I1EUdYLBPv9F_!!2185320355.png",
    ]

    public clawOpen1 = [
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01s9v96V1EUdYTb6WEA_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN018VllS61EUdYPhV8B4_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i1/2185320355/O1CN013kB6Q51EUdYRtfQlb_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN015sM4DD1EUdYNGxp66_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01A58Ktu1EUdYSxN1Nz_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01h7Hf2G1EUdYR5UQGh_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01fm3Ug31EUdYOpYpO7_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i2/2185320355/O1CN01sHxCRt1EUdYOuvbhL_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN0107MIIN1EUdYSxOlUk_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i2/2185320355/O1CN01xnP0ID1EUdYHQ27Ke_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i1/2185320355/O1CN01HVHNUX1EUdYOuwPbj_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01iTmw8L1EUdYQwhQ9h_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01Mo5RGj1EUdYP7V6Fw_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i2/2185320355/O1CN01QdvPVo1EUdYQUlswn_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01tIlwP71EUdYHQ1z1k_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i4/2185320355/O1CN01tXMAX11EUdYSCTMCx_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i1/2185320355/O1CN01joazcK1EUdYOutGBa_!!2185320355.png",
        "https://img.alicdn.com/imgextra/i3/2185320355/O1CN01Dl8OHP1EUdYP7Sgc5_!!2185320355.png",
    ]

    /** @prop {name:qiusNode, tips:"球的主节点", type:Node, default:Node}*/
    public qiusNode: Laya.Node
    /** @prop {name:coin,tips:"标识",type:Node}*/
    public coin: Laya.Node


    /** @prop {name:rocker, tips:"摇杆", type:Node, default:Node}*/
    public rocker: Laya.Node
    /** @prop {name:zhua, tips:"爪子", type:Node, default:Node}*/
    public zhua: Laya.Node
    /** @prop {name:rope,tips:"绳子",type:Node}*/
    public rope: Laya.Node
    /** @prop {name:newzhua,tips:"绳子",type:Node}*/
    public newzhua: Laya.Node
    /** @prop {name:getNUmBtn,tips:"获取抽奖机会按钮",type:Node}*/
    public getNUmBtn: Laya.Node
    /** @prop {name:downTimeNode,tips:"倒计时",type:Node}*/
    public downTimeNode: Laya.Node

    // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0

    private touchStart_x = 0;
    private moveSpeed = 8;
    private rotationSpeed = 1;
    private downSpeed = 0.4;
    private time = 10;
    private zhuaAni:Laya.Animation;
    private timeLine : Laya.TimeLine;
    private maskSp:Laya.Sprite

    constructor() { super(); }

    onEnable(): void {
        this.randownQiu();
        let that = this;
        if (Common.istaobao) {
            $global.loadOver();
            $global.startGame = function () {
                that.startGame();
            }
            $global.restartGame = function () {
                that.restartGame();
            }
            $global.clawDown = function () {
                that.clawDown();
            };
        } else {
            this.startGame();
        }
        (this.rocker.parent.getChildByName("coin") as Laya.Image).visible = false;

    }
    /**开始游戏 */
    startGame() {
        Common.isGameOver =false;
        (this.rocker.parent.getChildByName("coin") as Laya.Image).visible = true;
        this.downTime();
        this.initRocker();
    }
    /**初始化摇杆 */
    initRocker() {
        this.rocker.on(Laya.Event.MOUSE_DOWN, this, this.onDown);
        this.getNUmBtn.on(Laya.Event.CLICK, this, this.onGetNumBtn);
    }
    /**点击获取游戏机会 */
    onGetNumBtn() {
        if (Common.istaobao) {
            $global.getNumBtn();
        } else {
            this.clawDown();
        }
    }

    restartGame() {
        if (Common.type == 1) {
            Laya.Scene.open("OneScene.scene");
        } else if (Common.type == 2) {
            Laya.Scene.open("TwoScene.scene");
        }
    }
    /**倒计时*/
    downTime() {
        Laya.timer.loop(1000, this, this.doing);
    }
    timeDownAnimation() {
        this.timeLine = new Laya.TimeLine();
        this.timeLine.to(this.downTimeNode.parent, { rotation: -15 }, 50)
            .to(this.downTimeNode.parent, { rotation: 0 }, 50)
            .to(this.downTimeNode.parent, { rotation: 15 }, 50)
            .to(this.downTimeNode.parent, { rotation: 0 }, 50)
            .to(this.downTimeNode.parent, { rotation: -15 }, 50)
            .to(this.downTimeNode.parent, { rotation: 0 }, 50)
            .to(this.downTimeNode.parent, { rotation: 15 }, 50)
            .to(this.downTimeNode.parent, { rotation: 0 }, 50)
        this.timeLine.play(0, false);
        this.timeLine.on(Laya.Event.COMPLETE, this, () => {
            this.timeLine.reset();
        })
    }
    /**每秒执行*/
    doing() {
        this.time--;
        this.timeDownAnimation();
        if(this.time<=3){
            if(Common.istaobao){
                $global.timeDown();
            }
        }
        if (this.time <= 0) {
            //倒计时结束
            this.time = 0;
            Laya.timer.clear(this, this.doing);
            this.clawDown();
            (this.rocker as Laya.Image).rotation = 0;
        }
        (this.downTimeNode as Laya.Label).text = this.time + "s";
    }

    /**爪子下落 */
    clawDown() {
        if (Common.isGameOver) return;
        Common.isGameOver = true;
        switch (Common.type) {
            case 1:
                this.makeAnimation(this.zhuaAni, this.clawOpen, 60, this.newzhua, -15, -28, false, null);
                break;
            case 2:
                this.makeAnimation(this.zhuaAni, this.clawOpen1, 60, this.newzhua, -15, -28, false, null);
                break;
        }
        (this.newzhua.getChildByName("image1") as Laya.Image).visible=false;
         //给爪子添加碰撞体
        (this.coin as Laya.Image).visible = false;
        Laya.timer.clear(this, this.doing);
        this.newzhua.addComponent(boxCollider);
        Common.isMove = true;
        let BoxCollider = this.newzhua.addComponent(Laya.BoxCollider);
        BoxCollider.x = 35;
        BoxCollider.y = -30;
        BoxCollider.width = 10;
        BoxCollider.height = 50;
        BoxCollider.isSenor = true;
        let rigidbody = this.newzhua.addComponent(Laya.RigidBody);
        rigidbody.type = "kinematic";
        rigidbody.allowSleep = false;
        this.closeBtn();
    }
    onDown() {
        this.touchStart_x = Laya.stage.mouseX;
        (this.coin as Laya.Image).visible = false;
        Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.onMove);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.onUp);
    }
    onMove() {
        let pedestalNode = this.zhua as Laya.Image;
        let touchEnd_x = Laya.stage.mouseX;;
        let delt_x = touchEnd_x - this.touchStart_x;
        if (delt_x < 0) {
            pedestalNode.x -= this.moveSpeed;
            switch (Common.type) {
                case 1:
                    if (pedestalNode.x < 156) {
                        pedestalNode.x = 156;
                    }
                    break;
                case 2:
                    if (pedestalNode.x < -168) {
                        pedestalNode.x = -168;
                    }
                    break;
            }
            (this.rocker as Laya.Image).rotation -= this.rotationSpeed;
            if ((this.rocker as Laya.Image).rotation < -40) {
                (this.rocker as Laya.Image).rotation = -40;
            }
        } else {
            pedestalNode.x += this.moveSpeed;
            switch (Common.type) {
                case 1:
                    if (pedestalNode.x > 526) {
                        pedestalNode.x = 526;
                    }
                    break;
                case 2:
                    if (pedestalNode.x > 168) {
                        pedestalNode.x = 168;
                    }
                    break;
            }
    
            (this.rocker as Laya.Image).rotation += this.rotationSpeed;
            if ((this.rocker as Laya.Image).rotation > 40) {
                (this.rocker as Laya.Image).rotation = 40;
            }
        }
        this.touchStart_x = touchEnd_x;
    }
    onUp() {
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMove);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.onUp);
        (this.rocker as Laya.Image).rotation = 0;
    }
    /**随机球 */
    randownQiu() {
        let urls;
        switch (Common.type) {
            case 1:
                urls = Common.urlsOne;
                break;
            case 2:
                urls = Common.urlsTwo;
                break;
        }
        for (let i = 0; i < this.qiusNode.numChildren; i++) {
            let randown = Common.randomInt(0, 4);
            (this.qiusNode.getChildAt(i) as Laya.Image).skin = this.qiusImage[urls[randown][i]];
            (this.qiusNode.getChildAt(i) as Laya.Image).name = urls[randown][i] + "";
        }
    }

    onUpdate(): void {
        //绳子移动
        if (Common.isMove) {
            (this.rope as Laya.Image).height += this.downSpeed * Laya.timer.delta;
            (this.newzhua as Laya.Image).y += this.downSpeed * Laya.timer.delta;
        }
    }

    closeBtn() {
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMove);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.onUp);
        this.rocker.off(Laya.Event.MOUSE_DOWN, this, this.onDown);
    }

    /**创建动画
 * @param ani （可选）指定动画 
 * @param url （可选）指定动画播放开始的图片链接集合
 * @param interval （可选）播放速度。
 * @param parent (可选) 动画的父节点
 * @param pos_x  (可选) 指定动画的X坐标
 * @param pos_y  (可选) 指定动画的Y坐标
 * @param isloop (可选) 动画是否重复播放
 * @param isComplete  (可选) 是否开启返回动画
 * @override
*/
    makeAnimation(ani: Laya.Animation, url: string[], interval: number, parent: Laya.Node, pos_x: number, pos_y: number, isloop: boolean, isComplete: boolean) {
        ani = new Laya.Animation;
        parent.addChild(ani);
        parent.setChildIndex(ani, 1);
        ani.pos(pos_x, pos_y);
        ani.interval = interval;
        ani.loadImages(url).play(0, isloop);
        if (isComplete) {
            return ani
        } else {
            // ani.on(Laya.Event.COMPLETE, this, () => {
            //     for (let i = 0; i < url.length; i++) {
            //         Laya.loader.clearRes(url[i]);
            //     }
            //     ani.destroy();
            // })
        }
    }



    onDisable(): void {
    }
}